package com.andova.gltexture

import android.content.Context
import android.graphics.SurfaceTexture
import android.util.AttributeSet
import android.view.TextureView
import com.andova.egl.HT
import com.andova.egl.MultiThreadEglHelper
import com.andova.egl.OnTextureListener
import com.andova.ext.i

abstract class GLTextureView(context: Context, attrs: AttributeSet?) : TextureView(context, attrs), TextureView.SurfaceTextureListener {
    private val mEglHelper = MultiThreadEglHelper()
    final override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}
    final override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
        i("onSurfaceTextureSizeChanged -> width:$width,height:$height")
        mEglHelper.onSizeChanged(width, height)
    }

    final override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
        i("onSurfaceTextureDestroyed")
        mEglHelper.onSurfaceDestroy()
        surface.release()
        return true
    }

    final override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
        i("onSurfaceTextureAvailable -> width:$width,height:$height")
        mEglHelper.initMultiEgl(this, onTextureHT(), onTaskHT(), onTextureListener(), width, height)
    }

    fun getEglHelper(): MultiThreadEglHelper = mEglHelper
    abstract fun onTaskHT(): HT
    abstract fun onTextureHT(): HT
    abstract fun onTextureListener(): OnTextureListener
}